#!/usr/bin/env pwsh
# 企业微信第三方应用启动脚本
# WeChat Work Third-party Application Startup Script

Write-Host "=== 企业微信第三方应用启动脚本 ===" -ForegroundColor Green
Write-Host "WeChat Work Third-party Application Startup Script" -ForegroundColor Green
Write-Host ""

# 检查 .NET 8.0 SDK
Write-Host "检查 .NET 8.0 SDK..." -ForegroundColor Yellow
try {
    $dotnetVersion = dotnet --version
    Write-Host "✓ .NET SDK 版本: $dotnetVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 未找到 .NET SDK，请先安装 .NET 8.0 SDK" -ForegroundColor Red
    Write-Host "下载地址: https://dotnet.microsoft.com/download/dotnet/8.0" -ForegroundColor Yellow
    exit 1
}

# 检查项目文件
$apiProjectPath = "src/WeWorkApp.Api/WeWorkApp.Api.csproj"
if (-not (Test-Path $apiProjectPath)) {
    Write-Host "✗ 未找到后端项目文件: $apiProjectPath" -ForegroundColor Red
    exit 1
}

$webPath = "src/WeWorkApp.Web"
if (-not (Test-Path $webPath)) {
    Write-Host "✗ 未找到前端项目目录: $webPath" -ForegroundColor Red
    exit 1
}

Write-Host "✓ 项目文件检查完成" -ForegroundColor Green
Write-Host ""

# 还原 NuGet 包
Write-Host "还原 NuGet 包..." -ForegroundColor Yellow
Set-Location "src/WeWorkApp.Api"
try {
    dotnet restore
    Write-Host "✓ NuGet 包还原完成" -ForegroundColor Green
} catch {
    Write-Host "✗ NuGet 包还原失败" -ForegroundColor Red
    Set-Location "../.."
    exit 1
}

# 构建项目
Write-Host "构建后端项目..." -ForegroundColor Yellow
try {
    dotnet build --no-restore
    Write-Host "✓ 后端项目构建完成" -ForegroundColor Green
} catch {
    Write-Host "✗ 后端项目构建失败" -ForegroundColor Red
    Set-Location "../.."
    exit 1
}

Set-Location "../.."
Write-Host ""

# 检查配置文件
$configFile = "src/WeWorkApp.Api/appsettings.Development.json"
if (Test-Path $configFile) {
    Write-Host "✓ 找到开发环境配置文件" -ForegroundColor Green
} else {
    Write-Host "⚠ 未找到开发环境配置文件，将使用默认配置" -ForegroundColor Yellow
    Write-Host "建议创建 $configFile 并配置企业微信应用信息" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "=== 启动选项 ===" -ForegroundColor Cyan
Write-Host "1. 启动后端 API 服务"
Write-Host "2. 打开前端页面目录"
Write-Host "3. 同时启动后端并打开前端目录"
Write-Host "4. 查看项目信息"
Write-Host "5. 退出"
Write-Host ""

do {
    $choice = Read-Host "请选择操作 (1-5)"
    
    switch ($choice) {
        "1" {
            Write-Host "启动后端 API 服务..." -ForegroundColor Yellow
            Write-Host "API 服务将在 https://localhost:7001 启动" -ForegroundColor Green
            Write-Host "Swagger UI: https://localhost:7001/swagger" -ForegroundColor Green
            Write-Host "按 Ctrl+C 停止服务" -ForegroundColor Yellow
            Write-Host ""
            Set-Location "src/WeWorkApp.Api"
            dotnet run
            Set-Location "../.."
            break
        }
        "2" {
            Write-Host "打开前端页面目录..." -ForegroundColor Yellow
            $frontendPath = Resolve-Path "src/WeWorkApp.Web"
            Write-Host "前端目录: $frontendPath" -ForegroundColor Green
            Write-Host "请使用 Web 服务器托管此目录，或使用 VS Code Live Server 插件" -ForegroundColor Yellow
            Start-Process "explorer.exe" -ArgumentList $frontendPath
            break
        }
        "3" {
            Write-Host "同时启动后端并打开前端目录..." -ForegroundColor Yellow
            
            # 打开前端目录
            $frontendPath = Resolve-Path "src/WeWorkApp.Web"
            Start-Process "explorer.exe" -ArgumentList $frontendPath
            
            # 启动后端服务
            Write-Host "API 服务将在 https://localhost:7001 启动" -ForegroundColor Green
            Write-Host "Swagger UI: https://localhost:7001/swagger" -ForegroundColor Green
            Write-Host "前端目录已打开: $frontendPath" -ForegroundColor Green
            Write-Host "按 Ctrl+C 停止服务" -ForegroundColor Yellow
            Write-Host ""
            Set-Location "src/WeWorkApp.Api"
            dotnet run
            Set-Location "../.."
            break
        }
        "4" {
            Write-Host ""
            Write-Host "=== 项目信息 ===" -ForegroundColor Cyan
            Write-Host "项目名称: 企业微信第三方应用 - 客户详情查询系统"
            Write-Host "后端技术: .NET 8.0 Web API"
            Write-Host "前端技术: HTML5 + CSS3 + JavaScript"
            Write-Host "企业微信SDK: Senparc.Weixin.Work"
            Write-Host ""
            Write-Host "=== 目录结构 ===" -ForegroundColor Cyan
            Write-Host "src/WeWorkApp.Api/     - 后端 API 项目"
            Write-Host "src/WeWorkApp.Web/     - 前端 Web 项目"
            Write-Host "docs/                  - 项目文档"
            Write-Host ""
            Write-Host "=== 重要文件 ===" -ForegroundColor Cyan
            Write-Host "README.md              - 项目说明文档"
            Write-Host "WeWorkApp.sln          - Visual Studio 解决方案文件"
            Write-Host "start.ps1              - 项目启动脚本"
            Write-Host ""
            Write-Host "=== 配置说明 ===" -ForegroundColor Cyan
            Write-Host "1. 编辑 src/WeWorkApp.Api/appsettings.Development.json"
            Write-Host "2. 配置企业微信应用信息 (SuiteId, SuiteSecret, Token, EncodingAESKey)"
            Write-Host "3. 在企业微信管理后台配置回调URL和可信域名"
            Write-Host ""
            continue
        }
        "5" {
            Write-Host "退出脚本" -ForegroundColor Yellow
            break
        }
        default {
            Write-Host "无效选择，请输入 1-5" -ForegroundColor Red
            continue
        }
    }
} while ($choice -ne "5")

Write-Host ""
Write-Host "感谢使用企业微信第三方应用启动脚本！" -ForegroundColor Green
Write-Host "如有问题，请查看 README.md 文档或联系开发团队" -ForegroundColor Yellow